package com.ithinkstudio.jeez.config;

import com.ithinkstudio.jeez.generator.config.GenerateConfig;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * 数据源配置
 * @author felix
 */
@Configuration
public class GenerateDataSourceConfig {
    @Resource
    private GenerateConfig generateConfig;

    @Bean
    public DataSource dataSource() {
        return DataSourceBuilder.create()
                .driverClassName(generateConfig.getDriverClassName())
                .url(generateConfig.getUrl())
                .username(generateConfig.getUsername())
                .password(generateConfig.getPassword())
                .build();
    }

    @Bean
    public JdbcTemplate jdbcTemplate() {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource());
        return jdbcTemplate;
    }

    @Bean
    public NamedParameterJdbcTemplate namedParameterJdbcTemplate() {
        return new NamedParameterJdbcTemplate(jdbcTemplate());
    }

}
